﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using SampleImageGenerator.Core;

namespace SampleImageGenerator.Controls
{
	internal partial class EngineProgressBar : EngineListenerControl
	{
		public EngineProgressBar()
		{
			InitializeComponent();

			UpdateState(Engine); 
		}

		protected override void AttachEventHandlers(IEngine engine) 
		{
			engine.ProgressChanged += ProgressChangedHandler;
		}

		protected override void DetachEventHandlers(IEngine engine)
		{
			engine.ProgressChanged -= ProgressChangedHandler;
		}

		void ProgressChangedHandler(object sender, EventArgs e)
		{
			if (this.InvokeRequired)
			{
				Object[] args = new Object[] { sender, e };
				EventHandler handler = ProgressChangedHandler;
				this.Invoke(handler, args);
			}
			else
			{
				UpdateState(Engine); 
			}
		}

		protected override void UpdateState(IEngine engine) 
		{ 
			Int32 progress = engine.Progress;
			if (progress >= 0 && progress <= 100)
			{
				this.progressBar.Value = progress;
			}
		}
	}
}
